package cn.tedu.cart.service;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.databind.JsonMappingException;

import cn.tedu.common.pojo.Cart;
import cn.tedu.common.pojo.Product;
import cn.tedu.common.utils.MapperUtil;
//import cn.tedu.cart.mapper.CartMapper;
import redis.clients.jedis.JedisCluster;

@Service
public class CartService {
	// @Autowired
	// private CartMapper cartMapper;
	@Autowired
	private JedisCluster cluster;

	public List<Cart> queryMyCart(String userId) throws JsonParseException, JsonMappingException, IOException {
		// return cartMapper.queryMyCart(userId);
		// 构造对应set的key值
		String cartUserKey = "cart_user_" + userId;
		// 判断是否存在
		if (cluster.exists(cartUserKey)) {
			// 获取set中所有member smembers
			Set<String> productIds = cluster.smembers(cartUserKey);
			// 准备返回的List
			List<Cart> cartList = new ArrayList<Cart>();
			for (String productId : productIds) {
				// 拼接一个获取cartJson的key
				String cartKey = "cart_" + userId + productId;
				if (cluster.exists(cartKey)) {
					// 通过userId与productId定义一个cartJson
					String cartJson = cluster.get(cartKey);
					Cart cart = MapperUtil.MP.readValue(cartJson, Cart.class);
					cartList.add(cart);

				}
			}
			return cartList;
		} else {
			return null;
		}
	}

	@Autowired
	private RestTemplate client;

	public void saveCart(Cart cart) throws JsonParseException, JsonMappingException, IOException {
		/*
		 * 新增逻辑讨论 通过条件 user_id product_id 判断是否存在cart 不存在 新增,将数据补齐 insert 存在
		 * 更新数量,将已有的num和新增num求和更新num
		 */
		// 通过条件 userId,productId
		/*
		 * Cart exist=cartMapper.queryOne(cart); //判断已存在 if(exist==null){ //新增
		 * //insert into t_cart (字段) values(参数) //访问商品微服务 Product
		 * p=client.getForObject( "http://productservice/product/manage/item/"
		 * +cart.getProductId(), Product.class);
		 * cart.setProductName(p.getProductName());
		 * cart.setProductImage(p.getProductImgurl());
		 * cart.setProductPrice(p.getProductPrice()); cartMapper.saveCart(cart);
		 * }else{//有数据 exist封装了已存在的cart所有数据 //将num更新 老num+新num
		 * exist.setNum(exist.getNum()+cart.getNum());
		 * cartMapper.updateCartNum(exist); }
		 */

		// 判断新增的cart是否在redis中已存在
		String cartUserKey = "cart_user_" + cart.getUserId();
		// 判断商品id是否在这个set中存在
		// 存在则说明不新增,是更新num
		if (cluster.sismember(cartUserKey, cart.getProductId())) {
			// 进入if说明已存在,更新num
			String cartKey = "cart_" + cart.getUserId() + cart.getProductId();
			String cartJson = cluster.get(cartKey);
			Cart exist = MapperUtil.MP.readValue(cartJson, Cart.class);
			exist.setNum(exist.getNum() + cart.getNum());
			cluster.set(cartKey, MapperUtil.MP.writeValueAsString(exist));
		} else {
			// 不存在才新增
			Product p = client.getForObject("http://productservice/product/manage/item/" + cart.getProductId(),
					Product.class);
			cart.setProductName(p.getProductName());
			cart.setProductImage(p.getProductImgurl());
			cart.setProductPrice(p.getProductPrice());
			// 存redis
			String cartKey = "cart_" + cart.getUserId() + cart.getProductId();
			String cartJson = MapperUtil.MP.writeValueAsString(cart);
			// 既要使用userid productid存储cartJson
			// 也要为判断是否存在准备一个set中的新productId
			cluster.sadd(cartUserKey, cart.getProductId());
			cluster.set(cartKey, cartJson);
		}
	}

	public void updateCart(Cart cart) throws JsonParseException, JsonMappingException, IOException {
		// 不用判断存在的事
		String cartKey = "cart_" + cart.getUserId() + cart.getProductId();
		String cartJson = cluster.get(cartKey);
		Cart exist = MapperUtil.MP.readValue(cartJson, Cart.class);
		exist.setNum(cart.getNum());
		cluster.set(cartKey, MapperUtil.MP.writeValueAsString(exist));
		/* cartMapper.updateCartNum(cart); */
	}

	public void deleteCart(Cart cart) {
		// cartMapper.deleteCart(cart);
		String cartUserKey = "cart_user_" + cart.getUserId();
		String cartKey = "cart_" + cart.getUserId() + cart.getProductId();
		// 删除set中商品id
		cluster.srem(cartUserKey, cart.getProductId());
		// json字符串
		cluster.del(cartKey);
	}
}
